package com.itheima.reggie.contreller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;



    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        //获取登录用户的id
        Long userid = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,userid);
        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);
        return R.success(list);
    }


    @PostMapping("/add")
    public R<String> add(@RequestBody ShoppingCart shoppingCart){
    //设置用户id
        shoppingCart.setUserId(BaseContext.getCurrentId());
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        //判断添加的是菜品还是套餐
        if (shoppingCart.getDishId()!=null){
            //添加的是菜品
            queryWrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        }else{
            //添加的是套餐
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        //查询购物车是否已经存在
        ShoppingCart cart = shoppingCartService.getOne(queryWrapper);
        if (cart!=null){
            //数据库已经存在的数量加1
            cart.setNumber(cart.getNumber()+1);
            //修改数据
            shoppingCartService.updateById(cart);
            return R.success("添加成功");
        }else{
            //数量
            shoppingCart.setNumber(1);
            //创建时间
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            return R.success("添加成功");
        }

    }
    @DeleteMapping("/clean")
    public R<String> delete(){
        Long userid = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,userid);
        shoppingCartService.remove(queryWrapper);
        return R.success("删除成功");
    }
    @PostMapping("/sub")
    public R<String> sub(@RequestBody ShoppingCart shoppingCart){
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        if (shoppingCart.getDishId()!=null){
            queryWrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
            ShoppingCart one = shoppingCartService.getOne(queryWrapper);
            if (one.getNumber()>1){
                one.setNumber( one.getNumber()-1);
                shoppingCartService.updateById(one);
            }else{
                shoppingCartService.removeById(one);
            }
        }else{
            if (shoppingCart.getSetmealId()!=null){
                queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart);
                ShoppingCart cart = shoppingCartService.getOne(queryWrapper);
                if (cart.getNumber()>1){
                    cart.setNumber(cart.getNumber()-1);
                }else{
                    shoppingCartService.removeById(cart);
                }
            }
        }
           return R.success("修改成功");
    }
}
